#pragma once
#include <gui.hpp>
#include <lvgl.h>

/*!USER_DECLARE_BEGIN!*/
/*!USER_DECLARE_END!*/

// ---------------- 资源声明 ----------------
extern "C" {
// 字体声明
LV_FONT_DECLARE(lv_customer_font_montserratMedium_12)
LV_FONT_DECLARE(lv_customer_font_SourceHanSerifSC_Regular_13)
/*!USER_DECLARE_BEGIN!*/
/*!USER_DECLARE_END!*/
// 图片声明

/*!USER_DECLARE_BEGIN!*/
/*!USER_DECLARE_END!*/
// 其他
/*!USER_DECLARE_BEGIN!*/
/*!USER_DECLARE_END!*/
}

// ---------------- 组件定义 ----------------
namespace gui::widgets::main
{
    using namespace gui::compose;
    inline Component screen;
    inline Chart screen_chart_1;
    inline Button screen_btn_measure;
    inline Label screen_btn_measure_label;
    inline Button screen_btn_chart;
    inline Label screen_btn_chart_label;
    inline Button screen_btn_bug;
    inline Label screen_btn_bug_label;
    inline Label screen_label_1;
    inline Button screen_btn_datastore;
    inline Label screen_btn_datastore_label;
    inline Button screen_btn_blueteeth;
    inline Label screen_btn_blueteeth_label;
    inline Label screen_label_rin;
    inline Label screen_label_rout;
    inline Label screen_label_amplify;
    inline Label screen_label_bug;
    inline Label screen_label_fq;
    /*!USER_DECLARE_BEGIN!*/
    /*!USER_DECLARE_END!*/
}// namespace gui::widgets::main

// ================== 用户空间 ==================
/*!USER_DECLARE_BEGIN!*/

/*!USER_DECLARE_END!*/

// ---------------- UI接口 ----------------
namespace gui::interface
{
    using namespace gui::widgets::main;
    /*!USER_DECLARE_BEGIN!*/
    class CCT
    {
        static inline bool had_generated = false;
        static inline bool had_generated_bug = false;
        static inline bool had_generated_chart = false;
        static inline bool had_generated_store = false;
        static inline bool had_generated_bluetooth = false;
        // 添加生成状态标志
        static inline bool is_generating = false;
        static inline bool is_bug_generating = false;
        static inline bool is_chart_generating = false;
        static inline bool is_store_generating = false;
        static inline bool is_bluetooth_generating = false;
        static inline ChartSeries_t series{};   //数据 系列
        static inline ChartCursor_t cursor{};   //光标 系列1
        static inline lv_point_t cursor_point{};//光标 系列1
        static inline constexpr uint32_t point_count = 100;
        static inline int bug_id = 1;
        //测试用数组
        static inline std::array<int16_t, 100> amplify_freq_data = {};
    public:
        static auto initChartComponent()
        {
            screen_chart_1.update_mode(LV_CHART_UPDATE_MODE_SHIFT)// 改为SHIFT模式
                    .line_color(lv_color_hex(0x000000))
                    .point_count(point_count)
                    .remove_dot();

            series = screen_chart_1.add_series(lv_color_hex(0x34e6ff));

            screen_chart_1.line_color(lv_color_hex(0x808080))
                    .bg_color(lv_color_hex(0xFFFFFF))
                    .range(-2048, 2048);

            // cursor = screen_chart_1.add_cursor(lv_color_hex(0x0bce11), LV_DIR_ALL);
        }

        static auto generate_text(const float rin, const float rout, const float amp, const float fq)
        {
            auto set_label = [](auto &label, const char *fmt, auto value) {
                char buf[24];// 足够容纳任何浮点数
                snprintf(buf, sizeof(buf), fmt, value);
                label.text(buf);
            };

            set_label(screen_label_rin, "输入电阻：%.2fΩ", rin);
            set_label(screen_label_rout, "输出电阻：%.2fΩ", rout);
            set_label(screen_label_amplify, "增益：%.2f", amp);
            set_label(screen_label_fq, "上限频率：%.3fKHz", fq);
            set_label(screen_label_bug, "故障原因：无", 0);
        }
        static auto generate_fc_text(const float fc)
        {
            auto set_label = [](auto &label, const char *fmt, auto value) {
                char buf[24];// 足够容纳任何浮点数
                snprintf(buf, sizeof(buf), fmt, value);
                label.text(buf);
            };
            set_label(screen_label_fq, "上限频率：%.3fKHz", fc);
        }
        static auto generate_bug_text(uint8_t bug_id)
        {
            static constexpr std::array<const char*, 15> bug_texts = {
                    "故障原因：无",// 1
                    "故障原因：R1断路",// 2
                    "故障原因：R2断路",// 3
                    "故障原因：R3断路",// 4
                    "故障原因：R4断路",// 5
                    "故障原因：C1断路",// 6
                    "故障原因：C2断路",// 7
                    "故障原因：C3断路",// 8
                    "故障原因：R1短路",// 9
                    "故障原因：R2短路",// 10
                    "故障原因：R3短路",// 11
                    "故障原因：R4短路",// 12
                    "故障原因：C1双倍",// 13
                    "故障原因：C2双倍",// 14
                    "故障原因：C3双倍"// 15
            };

            if (bug_id >= 1 && bug_id <= bug_texts.size()) {
                screen_label_bug.text(bug_texts[bug_id-1]);
            } else {
                screen_label_bug.text("故障原因：未知故障");
            }
        }
        static auto clear_text()
        {
            screen_label_rin.text("输入电阻：-");
            screen_label_rout.text("输出电阻：-");
            screen_label_amplify.text("增益：-");
            screen_label_fq.text("上限频率：-");
        }
        static auto clear_bug_text()
        {
            screen_label_bug.text("故障原因：-");
        }


        template<std::size_t size>
        static auto generate_data(std::array<Coord,size>& arr)
        {
            if (size != point_count)
            {
                screen_chart_1.point_count(size);
            }
            screen_chart_1.ext_y_array(series, arr.data());
            // // chart数组填充测试
            // // get_amplify_freq().fill(1500);
            // for (int8_t i=0;i<point_count;i++) {
            //     get_amplify_freq()[i]=i;
            // }
            // screen_chart_1.range(0,200);
            // generate_data(get_amplify_freq());
        }

        static void setChartPoints(const uint16_t  nums)
        {
            screen_chart_1.point_count(nums);
        }

        // static auto generate_data_old(Coord* pool1, uint16_t size)
        // {
        //     if (size != point_count)
        //     {
        //         screen_chart_1.point_count(size);
        //     }
        //     screen_chart_1.ext_y_array(series, pool1);
        // }

        static auto toggle_generation()
        {
            is_generating = !is_generating;
            had_generated = true;
        }
        static auto get_generate_state() { return is_generating; }

        static auto toggle_generation_bug()
        {
            is_bug_generating = !is_bug_generating;
            had_generated_bug = true;
        }
        static auto get_generate_bug_state() { return is_bug_generating; }

        // 测试用函数
        static auto &get_amplify_freq()
        {
            return amplify_freq_data;
        }
        // 测试用函数 返回数据指针 - 明确使用函数调用
        static auto get_amplify_freq_ptr() noexcept
        {
            return amplify_freq_data.data();// 注意添加括号
        }
        static auto toggle_generation_chart()
        {
            is_chart_generating = !is_chart_generating;
            had_generated_chart = true;

        }
        static auto get_generate_chart_state() { return is_chart_generating; }
        static auto get_had_generate_chart_state() { return had_generated_chart; }

        //按一次触发一次 多次按状态不切换（虽然擦除会卡住应该不用管） 与前面逻辑不同
        static auto toggle_generation_store()
        {
            is_store_generating = true;
            had_generated_store = true;
        }
        static auto get_generate_store_state()
        {
            uint8_t store_flag = is_store_generating;
            is_store_generating = false;
            return store_flag;
        }
        static auto get_had_generate_store_state() { return had_generated_store; }

        static auto toggle_generation_bluetooth()
        {
            is_bluetooth_generating = !is_bluetooth_generating;
            had_generated_bluetooth = true;
        }
        static auto get_generate_bluetooth_state() { return is_bluetooth_generating; }
        static auto get_had_generate_bluetooth_state() { return had_generated_bluetooth; }

        static auto setChartRange(const Coord min, const Coord max)
        {
            screen_chart_1.range(min,max);
        }
    };


    /*!USER_DECLARE_END!*/
}// namespace gui::interface

// ---------------- 初始化逻辑 ----------------
namespace gui
{
    class ui
    {
    public:
        static void screenInit()
        {
            using namespace gui::widgets::main;

            screen.init(nullptr);
            screen.size(480, 320);
            screen.bg_opa(255);


            screen_chart_1.init(screen);
            screen_chart_1.pos(2, 33);
            screen_chart_1.size(329, 193);
            screen_chart_1.bg_opa(255);
            screen_chart_1.border_width(1);
            screen_chart_1.border_color(lv_color_hex(0xe8e8e8));
            screen_chart_1.line_width(2);
            screen_chart_1.line_color(lv_color_hex(0xe8e8e8));
            screen_chart_1.text_color(lv_color_hex(0x151212), static_cast<int>(LV_PART_TICKS) | LV_STATE_DEFAULT);
            screen_chart_1.text_opa(255, static_cast<int>(LV_PART_TICKS) | LV_STATE_DEFAULT);
            screen_chart_1.line_width(2, static_cast<int>(LV_PART_TICKS) | LV_STATE_DEFAULT);
            screen_chart_1.line_color(lv_color_hex(0xe8e8e8), static_cast<int>(LV_PART_TICKS) | LV_STATE_DEFAULT);
            screen_chart_1.line_opa(255, static_cast<int>(LV_PART_TICKS) | LV_STATE_DEFAULT);
            lv_chart_set_type(screen_chart_1, LV_CHART_TYPE_LINE);
            lv_chart_set_div_line_count(screen_chart_1, 16, 16);
            lv_chart_set_point_count(screen_chart_1, 5);
            lv_chart_set_zoom_x(screen_chart_1, 256);
            lv_chart_set_zoom_y(screen_chart_1, 256);
            lv_obj_set_style_text_font(screen_chart_1, &lv_customer_font_SourceHanSerifSC_Regular_13, static_cast<int>(LV_PART_TICKS) | LV_STATE_DEFAULT);

            screen_btn_measure.init(screen);
            screen_btn_measure.pos(369, 33);
            screen_btn_measure.size(81, 40);
            screen_btn_measure.bg_opa(255);
            screen_btn_measure.bg_color(lv_color_hex(0x1bc3ef));
            screen_btn_measure.radius(5);
            screen_btn_measure.text_color(lv_color_hex(0xffffff));
            screen_btn_measure.text_align(LV_TEXT_ALIGN_CENTER);
            lv_obj_set_style_pad_all(screen_btn_measure, 0, LV_STATE_DEFAULT);
            lv_obj_set_style_text_font(screen_btn_measure, &lv_customer_font_SourceHanSerifSC_Regular_13, selector_default);

            screen_btn_measure_label.init(screen_btn_measure);
            screen_btn_measure_label.center(0, 0);
            lv_label_set_text(screen_btn_measure_label, "测量");
            lv_obj_set_width(screen_btn_measure_label, LV_PCT(100));

            screen_btn_chart.init(screen);
            screen_btn_chart.pos(369, 84);
            screen_btn_chart.size(81, 40);
            screen_btn_chart.radius(5);
            screen_btn_chart.text_color(lv_color_hex(0xffffff));
            screen_btn_chart.text_align(LV_TEXT_ALIGN_CENTER);
            screen_btn_chart.bg_opa(255);
            screen_btn_chart.bg_color(lv_color_hex(0x1bc3ef));
            lv_obj_set_style_pad_all(screen_btn_chart, 0, LV_STATE_DEFAULT);
            lv_obj_set_style_text_font(screen_btn_chart, &lv_customer_font_SourceHanSerifSC_Regular_13, selector_default);

            screen_btn_chart_label.init(screen_btn_chart);
            screen_btn_chart_label.center(0, 0);
            lv_label_set_text(screen_btn_chart_label, "幅频特性曲线");
            lv_obj_set_width(screen_btn_chart_label, LV_PCT(100));

            screen_btn_bug.init(screen);
            screen_btn_bug.pos(369, 143);
            screen_btn_bug.size(81, 40);
            screen_btn_bug.bg_opa(255);
            screen_btn_bug.bg_color(lv_color_hex(0x1bc3ef));
            screen_btn_bug.radius(5);
            screen_btn_bug.text_color(lv_color_hex(0xffffff));
            screen_btn_bug.text_align(LV_TEXT_ALIGN_CENTER);
            lv_obj_set_style_pad_all(screen_btn_bug, 0, LV_STATE_DEFAULT);
            lv_obj_set_style_text_font(screen_btn_bug, &lv_customer_font_SourceHanSerifSC_Regular_13, selector_default);

            screen_btn_bug_label.init(screen_btn_bug);
            screen_btn_bug_label.center(0, 0);
            lv_label_set_text(screen_btn_bug_label, "故障分析");
            lv_obj_set_width(screen_btn_bug_label, LV_PCT(100));

            screen_label_1.init(screen);
            screen_label_1.pos(146, 8);
            screen_label_1.size(177, 19);
            screen_label_1.bg_opa(0);
            screen_label_1.text_align(LV_TEXT_ALIGN_CENTER);
            lv_label_set_text(screen_label_1, "简易电路特性测试仪");
            lv_obj_set_style_text_font(screen_label_1, &lv_customer_font_SourceHanSerifSC_Regular_13, selector_default);

            screen_btn_datastore.init(screen);
            screen_btn_datastore.pos(369, 197);
            screen_btn_datastore.size(81, 40);
            screen_btn_datastore.bg_opa(255);
            screen_btn_datastore.bg_color(lv_color_hex(0x1bc3ef));
            screen_btn_datastore.radius(5);
            screen_btn_datastore.text_color(lv_color_hex(0xffffff));
            screen_btn_datastore.text_align(LV_TEXT_ALIGN_CENTER);
            lv_obj_set_style_pad_all(screen_btn_datastore, 0, LV_STATE_DEFAULT);
            lv_obj_set_style_text_font(screen_btn_datastore, &lv_customer_font_SourceHanSerifSC_Regular_13, selector_default);

            screen_btn_datastore_label.init(screen_btn_datastore);
            screen_btn_datastore_label.center(0, 0);
            lv_label_set_text(screen_btn_datastore_label, "数据存储");
            lv_obj_set_width(screen_btn_datastore_label, LV_PCT(100));

            screen_btn_blueteeth.init(screen);
            screen_btn_blueteeth.pos(369, 253);
            screen_btn_blueteeth.size(81, 40);
            screen_btn_blueteeth.bg_opa(255);
            screen_btn_blueteeth.bg_color(lv_color_hex(0x1bc3ef));
            screen_btn_blueteeth.radius(5);
            screen_btn_blueteeth.text_color(lv_color_hex(0xffffff));
            screen_btn_blueteeth.text_align(LV_TEXT_ALIGN_CENTER);
            lv_obj_set_style_pad_all(screen_btn_blueteeth, 0, LV_STATE_DEFAULT);
            lv_obj_set_style_text_font(screen_btn_blueteeth, &lv_customer_font_SourceHanSerifSC_Regular_13, selector_default);

            screen_btn_blueteeth_label.init(screen_btn_blueteeth);
            screen_btn_blueteeth_label.center(0, 0);
            lv_label_set_text(screen_btn_blueteeth_label, "蓝牙收发");
            lv_obj_set_width(screen_btn_blueteeth_label, LV_PCT(100));

            screen_label_rin.init(screen);
            screen_label_rin.pos(2, 234);
            screen_label_rin.size(144, 15);
            screen_label_rin.text_align(LV_TEXT_ALIGN_LEFT);
            screen_label_rin.bg_opa(0);
            lv_label_set_text(screen_label_rin, "输入电阻：");
            lv_obj_set_style_text_font(screen_label_rin, &lv_customer_font_SourceHanSerifSC_Regular_13, selector_default);

            screen_label_rout.init(screen);
            screen_label_rout.pos(161, 234);
            screen_label_rout.size(144, 15);
            screen_label_rout.text_align(LV_TEXT_ALIGN_LEFT);
            screen_label_rout.bg_opa(0);
            lv_label_set_text(screen_label_rout, "输出电阻：");
            lv_obj_set_style_text_font(screen_label_rout, &lv_customer_font_SourceHanSerifSC_Regular_13, selector_default);

            screen_label_amplify.init(screen);
            screen_label_amplify.pos(2, 268);
            screen_label_amplify.size(144, 15);
            screen_label_amplify.text_align(LV_TEXT_ALIGN_LEFT);
            screen_label_amplify.bg_opa(0);
            lv_label_set_text(screen_label_amplify, "增益：");
            lv_obj_set_style_text_font(screen_label_amplify, &lv_customer_font_SourceHanSerifSC_Regular_13, selector_default);

            screen_label_bug.init(screen);
            screen_label_bug.pos(2, 298);
            screen_label_bug.size(144, 15);
            screen_label_bug.text_align(LV_TEXT_ALIGN_LEFT);
            screen_label_bug.bg_opa(0);
            lv_label_set_text(screen_label_bug, "故障原因：");
            lv_obj_set_style_text_font(screen_label_bug, &lv_customer_font_SourceHanSerifSC_Regular_13, selector_default);

            screen_label_fq.init(screen);
            screen_label_fq.pos(161, 268);
            screen_label_fq.size(144, 15);
            screen_label_fq.bg_opa(0);
            screen_label_fq.text_align(LV_TEXT_ALIGN_LEFT);
            lv_label_set_text(screen_label_fq, "上限频率值：");
            lv_obj_set_style_text_font(screen_label_fq, &lv_customer_font_SourceHanSerifSC_Regular_13, selector_default);

            /*!USER_DECLARE_BEGIN!*/
            using CCT = interface::CCT;
            CCT::initChartComponent();
            /*!USER_DECLARE_END!*/
            lv_obj_update_layout(screen);// 更新布局
            lv_scr_load(screen);         // 加载主屏幕
        }

        static void eventInit()
        {
            using namespace gui::widgets::main;
            /*!USER_DECLARE_BEGIN!*/
            using CCT = interface::CCT;
            screen_btn_measure.OnClicked([](lv_event_t *e) {
                CCT::toggle_generation();
            });
            screen_btn_bug.OnClicked([](lv_event_t *e) {
                CCT::toggle_generation_bug();
            });
            screen_btn_chart.OnClicked([](lv_event_t *e) {
                CCT::toggle_generation_chart();
            });
            screen_btn_datastore.OnClicked([](lv_event_t *e) {
                CCT::toggle_generation_store();
            });
            /*!USER_DECLARE_END!*/
        }
    };
}// namespace gui

/*!USER_DECLARE_BEGIN!*/
/*!USER_DECLARE_END!*/
